package org.jeecg.modules.demo.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CProjectApplication;
import org.jeecg.modules.demo.crm.entity.CReimbursement;
import org.jeecg.modules.demo.crm.entity.CTask;
import org.jeecg.modules.demo.crm.mapper.CProjectApplicationMapper;
import org.jeecg.modules.demo.crm.mapper.CReimbursementMapper;
import org.jeecg.modules.demo.crm.mapper.CTaskMapper;
import org.jeecg.modules.demo.crm.service.ICCommonService;
import org.jeecg.modules.demo.crm.service.ICReimbursementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Description: 报销表
 * @Author: jeecg-boot
 * @Date:   2025-06-20
 * @Version: V1.0
 */
@Service
public class CReimbursementServiceImpl extends ServiceImpl<CReimbursementMapper, CReimbursement> implements ICReimbursementService {

    @Autowired
    private CReimbursementMapper cReimbursementMapper;

    @Autowired
    private CTaskMapper cTaskMapper;

    @Autowired
    private CProjectApplicationMapper cProjectApplicationMapper;

    @Autowired
    private ICCommonService cCommonService;

    @Override
    public void edit(CReimbursement cReimbursement) {
        if("同意".equals(cReimbursement.getAuditStatus())
                || "驳回".equals(cReimbursement.getAuditStatus())){
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            cReimbursement.setAuditor(sysUser.getId());
            cReimbursement.setAuditorDate(new Date());
            cReimbursementMapper.updateById(cReimbursement);

        }else {
            if(StringUtils.isNotEmpty(cReimbursement.getTaskId())) {//不为空
                CTask task = cTaskMapper.selectById(cReimbursement.getTaskId());
                cReimbursement.setProjectId(task.getProjectId());
                cReimbursement.setSubProjectId(task.getSubProjectId());

                if (StringUtils.isNotEmpty(cReimbursement.getProjectId())) {
                    CProjectApplication cProjectApplication = cProjectApplicationMapper.selectById(cReimbursement.getProjectId());
                    if(cProjectApplication!=null){
                        cReimbursement.setCompanyId(cProjectApplication.getCompanyInfoId());
                    }

                }
            }

            cReimbursementMapper.updateById(cReimbursement);
        }

    }

    @Override
    public void audit(CReimbursement cReimbursement) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CReimbursement cReimbursement1 = cReimbursementMapper.selectById(cReimbursement.getId());
        if("待审核".equals(cReimbursement1.getAuditStatus())){
            cReimbursement1.setAuditor(sysUser.getId());
            cReimbursement1.setAuditStatus(cReimbursement.getAuditStatus());
            cReimbursement1.setAuditorDate(new Date());
            cReimbursement1.setAuditorDescription(cReimbursement.getDescription());
            cReimbursement1.setReceiptFile(cReimbursement.getReceiptFile());
            cReimbursementMapper.updateById(cReimbursement1);
        }
    }

    @Override
    public void editv2(CReimbursement cReimbursement) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CReimbursement cReimbursement1 = cReimbursementMapper.selectById(cReimbursement.getId());
        if("待审核".equals(cReimbursement1.getAuditStatus()) || "驳回".equals(cReimbursement1.getAuditStatus())){
            cReimbursement.setAuditStatus("待审核");
            cReimbursement.setUserId(sysUser.getId());
            cReimbursement.setAuditor("");
            cReimbursement.setAuditorDate(null);
            cReimbursement.setAuditorDescription("");
            cReimbursement.setReceiptFile("");
            cReimbursementMapper.updateById(cReimbursement);
        }

    }

    @Override
    public Map<String, Long> statusCount(CReimbursement cReimbursement, HttpServletRequest req) {
        CReimbursement query = new CReimbursement();
        Map<String, Long> data = new HashMap<>();
        List<String> statusList = new ArrayList<>();
        statusList.add("待审核");
        statusList.add("同意");
        statusList.add("驳回");
        statusList.add("全部");
        QueryWrapper<CReimbursement> queryWrapper = null;
        Boolean permissionCheck = cCommonService.taskpermissionCheck();
        for (String status : statusList) {
            queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
            if(!status.equals("全部") ){
                queryWrapper.eq("audit_status", status);
            }
            if(cReimbursement != null && cReimbursement.getProjectId() != null){
                queryWrapper.eq("project_id",cReimbursement.getProjectId());
            }
            if(cReimbursement != null && cReimbursement.getSubProjectId()!= null){
                queryWrapper.eq("sub_project_id",cReimbursement.getSubProjectId());
            }
            if(cReimbursement != null && cReimbursement.getCompanyId()!= null){
                queryWrapper.eq("company_id",cReimbursement.getCompanyId());
            }
            if(cReimbursement != null && cReimbursement.getReimbursementName()!= null){
                queryWrapper.eq("reimbursement_name",cReimbursement.getReimbursementName());
            }
            if(permissionCheck){
                LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                queryWrapper.eq("user_id",sysUser.getId());
            }
            data.put(status,cReimbursementMapper.selectCount(queryWrapper));
        }
        return data;
    }
}
